{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def fun(listB):\n",
    "    print(\"listB\",listB)\n",
    "    listB = [4,5,6]\n",
    "    print(\"listB\",listB)\n",
    "\n",
    "\n",
    "listA = [1,2,3]\n",
    "print(\"listA\",listA)\n",
    "fun(listA)\n",
    "print(\"listA\",listA)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def change(listB):\n",
    "    print(\"listB\",listB)\n",
    "    listB[0] = 4\n",
    "    print(\"listB\",listB)\n",
    "\n",
    "\n",
    "listA = [1,2,3]\n",
    "print(\"listA\",listA)\n",
    "change(listA)\n",
    "print(\"listA\",listA)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 不同类型的参数\n",
    "\n",
    "# 1. 根据位置次序\n",
    "def bmi(weight,height):\n",
    "    print(\"{:.2f}\".format(weight/height**2))\n",
    "\n",
    "print(bmi(80,1.72))\n",
    "\n",
    "# 2. 根据关键字\n",
    "print(bmi(height=1.72,weight=80))\n",
    "\n",
    "# 3. 可选参数\n",
    "def mul(x,y=20):\n",
    "    return x*y\n",
    "\n",
    "print(mul(10,30))\n",
    "print(mul(10,20))\n",
    "print(mul(10))\n",
    "\n",
    "# 4. 可变参数\n",
    "# def add(a,b,c,d):\n",
    "    # return a+b+c+d\n",
    "\n",
    "def add(*a):\n",
    "    result = 0\n",
    "    for n in a:\n",
    "        result += n\n",
    "    return result\n",
    "\n",
    "print(add(1,2,3))\n",
    "print(add(1,2,3,4,5,6))\n",
    "\n",
    "# 5. 可变长关键字参数\n",
    "\n",
    "def foo(**info):\n",
    "    print(info)\n",
    "\n",
    "foo(id=1001)\n",
    "foo(id=1001,name='张三')\n",
    "foo(id=1001,name='张三',age=18)\n",
    "\n",
    "# 练习:\n",
    "# 定义一个函数 实现求最小值 参数数量不定\n",
    "print(min(100,3000))\n",
    "print(min(1,-1,3))\n",
    "print(min(12,23,34,2334,97,234))\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add(x,y):\n",
    "    return x+y\n",
    "\n",
    "# 匿名函数的写法\n",
    "add2 = lambda x,y:x+y\n",
    "\n",
    "print(add(10,20))\n",
    "print(add2(10,20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls = [('zhangsan',33),('lisi',99),('wangwu',60),('zhaoliu',72)]\n",
    "\n",
    "ls.sort(key=lambda x:x[0],reverse=False)\n",
    "\n",
    "print(ls)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 求 n!\n",
    "\n",
    "# 求 5!  = 5 * 4 * 3 * 2 * 1\n",
    "\n",
    "def foo1(n):\n",
    "    result = 1\n",
    "    for i in range(1,n+1):\n",
    "        result *= i\n",
    "    print(result)\n",
    "\n",
    "foo1(5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5! -> 5 * 4! \n",
    "# 4! -> 4 * 3!\n",
    "# 3! -> 3 * 2!\n",
    "# 2! -> 2 * 1!\n",
    "# 1! -> 1\n",
    "\n",
    "\n",
    "\n",
    "# 5! -> 5 * 4! = 5*24 = 120\n",
    "# 4! -> 4 * 3! = 4*6 = 24\n",
    "# 3! -> 3 * 2! = 3* 2 = 6\n",
    "# 2! -> 2 * 1  = 2\n",
    "\n",
    "def foo(n):\n",
    "    print(\"foo\",n)\n",
    "    if n>1:\n",
    "        # 转换为较小问题\n",
    "        return n * foo(n-1)\n",
    "    else:\n",
    "        return 1\n",
    "\n",
    "print(foo(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
